/*
 * FreeSpaceFileTest.cpp
 *
 *  Created on: Oct 27, 2013
 *      Author: SG0214746
 */

#include "FreeSpaceFileTest.h"
#include "../persistence/blockManagment/FreeSpaceFile.h"
#include <stdio.h>
#include <iostream>

FreeSpaceFileTest::FreeSpaceFileTest(const String& name) : name(name) {}

FreeSpaceFileTest::~FreeSpaceFileTest() {}

void FreeSpaceFileTest::run() const {
	std::cout << "Pruebas sobre la clase FreeSpaceFile" << std::endl;
	addFreeBlocks();
	disposeBlocks();
	readMultipleBlocksOfFreeSpace();
	std::cout << std::endl;
}

void FreeSpaceFileTest::addFreeBlocks() const {
	std::cout << "reserva de espacio de bloque" << std::endl;
	FreeSpaceFile file(3, 512);
	file.open(name);
	file.push(100);
	file.push(50);
	file.push(80);

	std::cout << "{" << file.getFreeSpace(0) << "}{" << file.getFreeSpace(1) << "}{" << file.getFreeSpace(2) << "}" << std::endl;
	file.close();
	removeFile();
}

void FreeSpaceFileTest::disposeBlocks() const {
	std::cout << "liberacion de espacio de bloque" << std::endl;
	FreeSpaceFile file(3, 512);
	file.open(name);
	file.push(100);
	std::cout << "{" << file.getFreeSpace(0) << "}" << std::endl;
	file.setFreeSpace(0, 50);
	std::cout << "{" << file.getFreeSpace(0) << "}" << std::endl;
	file.close();
	removeFile();
}

void FreeSpaceFileTest::readMultipleBlocksOfFreeSpace() const {
	std::cout << "lectura de varios bloques de espacios libres" << std::endl;
	FreeSpaceFile file(1, 512);
	file.open(name);
	file.push(100);
	file.push(50);
	file.push(80);

	std::cout << "{" << file.getFreeSpace(0) << "}{" << file.getFreeSpace(1) << "}{" << file.getFreeSpace(2) << "}" << std::endl;
	file.close();
	removeFile();
}

void FreeSpaceFileTest::removeFile() const {
	if(::remove(name.c_str()) != 0 )
		std::cout <<  "Error deleting file" << std::endl;
	else
	    std::cout <<  "File successfully deleted" << std::endl;
}
